/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.sysAdmin;

import controllers.utils.DatabaseHjelper;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import models.Department;
import models.StudyProgram;

/**
 *
 * @author dybvik
 */
@SessionScoped
@Named(value="newStudyProgram")
public class addNewStudyprogram implements Serializable{
    private StudyProgram newStudprog;
    private final List<Department> departments = new ArrayList<Department>();
    private DatabaseHjelper dbh = DatabaseHjelper.hent();

    /** Creates a new instance of executeNewStudyprogram */
    public addNewStudyprogram() {
        this.newStudprog = new StudyProgram();
        if(departments.isEmpty()) updateDepartments();
    }

    private void updateDepartments() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("select * from department");
            res = stmt.executeQuery();
            synchronized (departments) {
                departments.clear();
                while (res.next()){
                    Department dep = new Department();
                    dep.setDepartmentID(res.getInt("departmentID"));
                    dep.setName(res.getString("departmentName"));
                    dep.setAddress(res.getString("depAddress"));
                    dep.setPhoneNr(res.getString("depPhoneNr"));
                    dep.getInstitute().setInstituteID(res.getInt("instituteID"));
                    dep.setNote(res.getString("depNote"));
                    departments.add(dep);
                }
                for(Department dep: departments){
                    stmt = conn.prepareStatement("select * from institute where instituteID=?");
                    stmt.setInt(1, dep.getInstitute().getInstituteID());
                    res = stmt.executeQuery();
                    dep.getInstitute().setName(res.getString("instituteName"));
                }

            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            DatabaseHjelper.lukk(res);
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    public String addNewStudyProgram(){
        newStudprog.setStudyProgramCode(0);
        newStudprog.setName(null);
        newStudprog.setApproved(null);
        newStudprog.setApprovedBy(null);
        newStudprog.setLastRevised(null);
        newStudprog.setIsNKR(false);
        updateDepartments();
        return "addNewStudyProgram";
    }

    public String executeNewStudyProgram(){
        addNewStudyProgramToDB();
        return "addNewStudyProgramOutput";
    }

    private void addNewStudyProgramToDB() {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("INSERT INTO studyprogram(studyProgramCode, studyProgramName, departmentID, approved, lastRevised, isNKR, approvedBy) VALUES(?, ?, ?, ?, ?, ?, ?)");
            stmt.setInt(1, newStudprog.getStudyProgramCode());
            stmt.setString(2, newStudprog.getName());
            stmt.setInt(3, newStudprog.getDepartment().getDepartmentID());
            stmt.setDate(4, javaDateToSqlDate(newStudprog.getApproved()));
            stmt.setDate(5, javaDateToSqlDate(newStudprog.getLastRevised()));
            stmt.setInt(6, newStudprog.isIsNKRAsInt());
            stmt.setString(7, newStudprog.getApprovedBy());
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Feil i lagring av StudyProgram: " + e);
        } finally {
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    private java.sql.Date javaDateToSqlDate(Date d) {
        long ms = d.getTime();
        java.sql.Date sqlDato = new java.sql.Date(ms);
        return sqlDato;
    }

    public StudyProgram getNewStudprog() {
        return newStudprog;
    }

    public List<Department> getDepartments() {
        return departments;
    }

    public void setDepartments(int departmentID) {
        System.out.println("Department ID som SKAL settes i newStudprogget: " + departmentID);
        newStudprog.getDepartment().setDepartmentID(departmentID);
        System.out.println("DepID som ER satt i newStudprog.: " + newStudprog.getDepartment().getDepartmentID());
        for(Department dep: departments){
            System.out.println("Dep-navn i newStudprog FØR menyen setter det: " + newStudprog.getDepartment().getName());
            if(dep.getDepartmentID()==departmentID) newStudprog.getDepartment().setName(dep.getName());
            System.out.println("Dep-navn i newStudprog FETTER ETTER menyen setter det: " + newStudprog.getDepartment().getName());
        }
    }
    
}
